package edu.cmu.scs.hci.TagIt;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class PhotoCursorAdapter extends BaseAdapter {
	
	private Cursor mCursor;
	private Context mContext;
	
	public PhotoCursorAdapter(Context context, Cursor cursor) {
		mContext = context;
		mCursor = cursor;
	}
	public int getCount() {
		return mCursor.getCount();
	}

	public Object getItem(int position) {
		mCursor.moveToPosition(position);
		if (mCursor.isBeforeFirst() || mCursor.isAfterLast())
			return null;
		String uri_string = mCursor.getString(mCursor.getColumnIndex(PhotosDbAdapter.KEY_URI));
		Uri uri = Uri.parse(uri_string);
		return uri;
	}

	public long getItemId(int position) {
		mCursor.moveToPosition(position);
		if (mCursor.isBeforeFirst() || mCursor.isAfterLast())
			return -1;
		return mCursor.getLong(mCursor.getColumnIndex(PhotosDbAdapter.KEY_ID));
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Uri uri = (Uri) getItem(position);
		if (convertView != null) {
			ImageView imView = (ImageView) convertView;
			imView.setImageURI(uri);
			return imView;
		}
		else {
			ImageView imView = new ImageView(mContext);
			imView.setImageURI(uri);
			imView.setAdjustViewBounds(true);
			imView.setMaxHeight(100);
			imView.setMaxWidth(100);
			return imView;
		}
	}
}
